import 'package:qyou_app/common/api.dart';
import 'package:qyou_app/common/base.dart';
import 'package:qyou_app/common/bloc.dart';
import 'package:qyou_app/common/http.dart';
import 'package:qyou_app/model/carousel.dart';
import 'package:qyou_app/model/channel.dart';
import 'package:qyou_app/widget/countdown.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';

/*
 * app启动界面
 */
class StartPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _StartPageState();
}

class _StartPageState extends State<StartPage> with Base {
  SharedPreferences pres;

  //app启动预加载首页数据
  @override
  void initState() {
    super.initState();
    _init();
    _getNavData();
    _getCarouselData();
    _getVideoData();
  }

  //获取本地存储数据
  void _init() async {
    pres = await SharedPreferences.getInstance();
  }

  //获取导航列表信息
  void _getNavData() {
    HttpKit().get(Api.nav).then((value) {
      var navMap = value.data;
      List<Channel> navList = (navMap as List<dynamic>)
          .map((e) => Channel.fromJson((e as Map<String, dynamic>)))
          .toList();
      BlocProvider.of<NavCubit>(context).updateNavData(navList);
    });
  }

  //获取轮播图信息
  void _getCarouselData() {
    HttpKit().get(Api.carousel).then((value) {
      var carouselMap = value.data;
      List<Carousel> carouselList = (carouselMap as List<dynamic>)
          .map((e) => Carousel.fromJson((e as Map<String, dynamic>)))
          .toList();
      BlocProvider.of<CarouselCubit>(context).updateCarouselData(carouselList);
    });
  }

  //获取视频接口
  _getVideoData() async {
    List videoList = await Api.getVideoListData(context);
    BlocProvider.of<VideoCubit>(context).updateVideoData(videoList);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox(
        width: MediaQuery.of(context).size.width, //获取屏幕宽度
        child: Stack(
          alignment: Alignment.center,
          children: <Widget>[
            SizedBox(
              width: dp(375),
              child: Image.asset("assets/start.jpg"),
            ),
            Positioned(
              right: dp(40),
              top: dp(40),
              child: CountDown(),
            ),
          ],
        ),
      ),
    );
  }
}
